# @Time : 2021/6/2 10:56
# @Author : Fioman 
# @Phone : 13149920693
import time

from settings import MSG_SEND_PATH, MSG_RECV_PATH, EAP_SEND_MOCK_PATH
from xml.dom.minidom import parse, Document

"""
xml文件解析工具,将要发送的内容写入到固定的文件中
"""


def get_transacton_id():
    """
    获取当前的时间字符串,加上后面三位毫秒
    :return:
    """
    nowSecond = time.time()
    lastThree = str(nowSecond).split(".")[1][:3]
    now = time.strftime("%Y%m%d%H%M%S", time.localtime(nowSecond))
    transactionId = now + lastThree
    return transactionId


def pack_eap_mock_send_msg(msgHeader, bodyDict, retCode):
    """
    将要发送的消息进行压缩.写入到固定路径的xml文件中去
    :param msgHeader:消息的头部内容
    :param bodyDict:消息的主体部分
    :param retCode:返回的消息内容
    :return:
    """
    doc = Document()
    root = doc.createElement("message")
    doc.appendChild(root)

    # 先将header的节点加上去
    headNode = doc.createElement("header")
    msgNameNode = doc.createElement("messagename")
    msgNameNode.appendChild(doc.createTextNode(msgHeader))
    transactionId = get_transacton_id()
    idNode = doc.createElement("transactionid")
    idNode.appendChild(doc.createTextNode(transactionId))
    headNode.appendChild(msgNameNode)
    headNode.appendChild(idNode)

    # 再将body的节点的内容写上去
    bodyNode = doc.createElement("body")
    dataNode = doc.createElement("data")
    for key, val in bodyDict.items():
        nodeName = key
        nodeVal = val
        nodeAdd = doc.createElement(nodeName)
        nodeAdd.appendChild(doc.createTextNode(nodeVal))
        dataNode.appendChild(nodeAdd)
    bodyNode.appendChild(dataNode)

    # 将ret的节点的内容写上去
    retNode = doc.createElement("return")
    for element, value in retCode.items():
        node = doc.createElement(element)
        if value != "":
            node.appendChild(doc.createTextNode(value))
        retNode.appendChild(node)

    root.appendChild(headNode)
    root.appendChild(bodyNode)
    root.appendChild(retNode)

    fp = open(EAP_SEND_MOCK_PATH, "w", encoding="UTF-8")
    doc.writexml(fp, indent='', addindent='\t', newl='\n', encoding='UTF-8')
    fp.close()


def pack_send_msg(msgHeader, bodyDict, retCode):
    """
    将要发送的消息进行压缩.写入到固定路径的xml文件中去
    :param msgHeader:消息的头部内容
    :param bodyDict:消息的主体部分
    :param retCode:返回的消息内容
    :return:
    """
    doc = Document()
    root = doc.createElement("message")
    doc.appendChild(root)

    # 先将header的节点加上去
    headNode = doc.createElement("header")
    msgNameNode = doc.createElement("messagename")
    msgNameNode.appendChild(doc.createTextNode(msgHeader))
    transactionId = get_transacton_id()
    idNode = doc.createElement("transactionid")
    idNode.appendChild(doc.createTextNode(transactionId))
    headNode.appendChild(msgNameNode)
    headNode.appendChild(idNode)

    # 再将body的节点的内容写上去
    bodyNode = doc.createElement("body")
    dataNode = doc.createElement("data")
    for key, val in bodyDict.items():
        nodeName = key
        nodeVal = val
        nodeAdd = doc.createElement(nodeName)
        nodeAdd.appendChild(doc.createTextNode(nodeVal))
        dataNode.appendChild(nodeAdd)
    bodyNode.appendChild(dataNode)

    # 将ret的节点的内容写上去
    retNode = doc.createElement("return")
    for element, value in retCode.items():
        node = doc.createElement(element)
        if value != "":
            node.appendChild(doc.createTextNode(value))
        retNode.appendChild(node)

    root.appendChild(headNode)
    root.appendChild(bodyNode)
    root.appendChild(retNode)

    fp = open(MSG_SEND_PATH, "w", encoding="UTF-8")
    doc.writexml(fp, indent='', addindent='\t', newl='\n', encoding='UTF-8')
    fp.close()


def parse_recv_msg(msgRecv):
    """
    解析接收到的消息,并写入到xml文件中.
    :param msgRecv: 接收到全部的二进制数据,包括首位两部分ch(2)和ch(3)
    :return:
    """
    with open(MSG_RECV_PATH, "wb") as f:
        f.write(msgRecv[1:-1])

    dom = parse(MSG_RECV_PATH)
    rootNode = dom.documentElement
    msgNameNode = rootNode.getElementsByTagName("messagename")[0]
    bodyDict = {}
    bodyNode = rootNode.getElementsByTagName("body")[0]
    if len(bodyNode.childNodes) > 0:
        dataNode = bodyNode.getElementsByTagName("data")[0]
        for node in dataNode.childNodes:
            if node.nodeName != "#text":
                bodyDict[node.nodeName] = node.childNodes[0].data

    retNode = rootNode.getElementsByTagName("returncode")[0]
    msgName = msgNameNode.childNodes[0].data
    retCode = retNode.childNodes[0].data
    return msgName, bodyDict, retCode
